;var ImageCropper = function ($) {

    // 定义常用常量
    var NAME = 'imageCropper';
    var DATA_KEY = 'imageCropper';
    var EVENT_KEY = '.' + DATA_KEY;
    var DATA_API_KEY = '.data-api';
    var JQUERY_NO_CONFLICT = $.fn[NAME];

    var Default = {
        fileSelector: '', // 上传文件选择器
        confirmSelector: '', // 确定选择器
        preview: '', // 预览选择器
        autoCrop: true, // 是否自动裁剪
        autoCropArea: 0.5, // 自动裁剪区域
        viewMode: 1, // 视图模式
        rotatable: false,

        // 加载图片前处理
        onLoad: function () {
        },

        // 选择图片
        onSelect: function () {
        },

        // 确认按钮
        onConfirm: function (e) {
        }
    };

    var ImageCropper = function () {

        function ImageCropper(element, config) {
            if (!$().cropper) {
                throw new Error("当前上下文不包含 $().cropper 插件。");
            }

            this._element = element;
            this._config = $.extend({}, Default, config);
            this._previews = $(this._config.preview);
            this._previewURL = null;

            this.init();
        }

        ImageCropper.prototype.init = function () {
            if (!this._config.ready) {
                this._config.ready = ready;
            }
            if (!this._config.crop) {
                this._config.crop = crop;
            }

            var config = this._config;
            var element = this._element;
            var previews = this._previews;
            if (config.onLoad) {
                config.onLoad();
            }
            $(element).cropper(config);

            $(config.fileSelector).on('change', function () {
                var file, files = $(this)[0].files;
                if (files && files.length) {
                    file = files[0];

                    if (config.onSelect) {
                        config.onSelect();
                    }

                    if (/^image\/\w+$/.test(file.type)) {
//						if (this._previewURL) {
//							URL.revokeObjectURL(this._previewURL);
//						}
//						this._previewURL = URL.createObjectURL(file);
//						$(element).attr('src', this._previewURL).show();
//						$(element).cropper('replace', this._previewURL);

                        var fr = new FileReader();
                        fr.onload = function () {
                            $(element).attr('src', fr.result).show();
                            $(element).cropper('replace', fr.result);
                        };
                        fr.readAsDataURL($(this)[0].files[0]);
                        $(this).val(null);
                    } else {
                        $(this).val(null);
                        window.alert('请选择有效的图片文件');
                    }
                }
            });

            $(config.confirmSelector).on('click', function () {
                var canvas = $(element).cropper('getCroppedCanvas');
                if (config.onConfirm) {
                    console.debug('超过 1M 的文件上传可能会失败。');
                    config.onConfirm(canvas);
                }
            });

            function ready() {
                var $clone = $(element).clone().removeClass('cropper-hidden');

                $clone.css({
                    display: 'block',
                    width: '100%',
                    minWidth: 0,
                    minHeight: 0,
                    maxWidth: 'none',
                    maxHeight: 'none'
                });

                resetPreview();
                previews.html($clone);
            }

            function resetPreview() {
                previews.css({
                    width: '100%',
                    overflow: 'hidden'
                });
            }

            function crop(e) {
                var imageData = $(element).cropper('getImageData');
                var previewAspectRatio = e.width / e.height;

                resetPreview();
                previews.each(function (index) {
                    var currentPreview = previews.eq(index);
                    var previewWidth = currentPreview.width();
                    var previewHeight = previewWidth / previewAspectRatio;
                    var imageScaledRatio = e.width / previewWidth;

                    currentPreview.height(previewHeight);
                    currentPreview.find('img').css({
                        width: imageData.naturalWidth / imageScaledRatio,
                        height: imageData.naturalHeight / imageScaledRatio,
                        marginLeft: -e.x / imageScaledRatio,
                        marginTop: -e.y / imageScaledRatio
                    });
                });
            }
        };

        ImageCropper.prototype.data = function () {
            return $(this._element).cropper('getCroppedCanvas');
        };

        ImageCropper.prototype.reset = function () {
            $(this._element).cropper('destroy');
        };

        ImageCropper._jQueryInterface = function (config) {
            var $elem = $(this);
            var data = $elem.data(DATA_KEY);
            if (!data) {
                data = new ImageCropper(this, config);
                $elem.data(DATA_KEY, data);
            }

            return data;
        };

        return ImageCropper;
    }();

    $.fn[NAME] = ImageCropper._jQueryInterface;
    $.fn[NAME].Constructor = ImageCropper;
    $.fn[NAME].noConflict = function () {
        $.fn[NAME] = JQUERY_NO_CONFLICT;
        return ImageCropper._jQueryInterface;
    };
    return ImageCropper;
}(jQuery);